package com.awplanet.sitegen;


import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * User: a_polyakov
 */
public class SiteGenFrame extends JFrame implements ActionListener, ChangeListener {
    private JTextField sourceDirField;
    private JButton sourceDirButton;
    private JCheckBox includeTestCheckBox;
    private JCheckBox includeWordDataCheckBox;
    private JCheckBox includeUsaDataCheckBox;
    private JCheckBox includeMoneyGramDataCheckBox;
    private JTextField targetDirField;
    private JButton targetDirButton;
    private JTextField indexPageSizeField;
    private JTextField countryPageSizeField;
    private JTextField cityPageSizeField;
    private JTextField minRandomLinkField;
    private JTextField maxRandomLinkField;
    private JTextField minRandomTextField;
    private JTextField maxRandomTextField;
    private JButton generateButton;
    private JProgressBar progressBar;

    public SiteGenFrame() throws HeadlessException {
        super("Generate Web Site with address");
        setSize(500, 330);
        setMinimumSize(new Dimension(350, 300));

        JPanel panel = new JPanel(new BorderLayout());
        JPanel labelPanel = new JPanel(new GridLayout(13, 1));
        JPanel fieldPanel = new JPanel(new GridLayout(13, 1));
        JPanel buttonPanel = new JPanel(new GridLayout(13, 1));

        labelPanel.add(new JLabel("Source dir"));
        sourceDirField = new JTextField(new File("").getAbsolutePath());
        fieldPanel.add(sourceDirField);
        sourceDirButton = new JButton("Choice");
        sourceDirButton.addActionListener(this);
        buttonPanel.add(sourceDirButton);

        labelPanel.add(new JLabel("Include"));
        fieldPanel.add(new JLabel("TEST"));
        includeTestCheckBox =new JCheckBox();
        buttonPanel.add(includeTestCheckBox);

        labelPanel.add(new JLabel("Include"));
        fieldPanel.add(new JLabel("word2.txt"));
        includeWordDataCheckBox =new JCheckBox();
        buttonPanel.add(includeWordDataCheckBox);

        labelPanel.add(new JLabel("Include"));
        fieldPanel.add(new JLabel("usa2.txt"));
        includeUsaDataCheckBox =new JCheckBox();
        buttonPanel.add(includeUsaDataCheckBox);

        labelPanel.add(new JLabel("Include"));
        fieldPanel.add(new JLabel("final_moneygram_formated.txt"));
        includeMoneyGramDataCheckBox =new JCheckBox();
        buttonPanel.add(includeMoneyGramDataCheckBox);

        labelPanel.add(new JLabel("Target dir"));
        targetDirField = new JTextField(sourceDirField.getText() + "/gen");
        fieldPanel.add(targetDirField);
        targetDirButton = new JButton("Choice");
        targetDirButton.addActionListener(this);
        buttonPanel.add(targetDirButton);

        labelPanel.add(new JLabel("Page size for index"));
        indexPageSizeField = new JTextField("0");
        fieldPanel.add(indexPageSizeField);

        labelPanel.add(new JLabel("Page size for country"));
        countryPageSizeField = new JTextField("0");
        fieldPanel.add(countryPageSizeField);

        labelPanel.add(new JLabel("Page size for city"));
        cityPageSizeField = new JTextField("0");
        fieldPanel.add(cityPageSizeField);

        labelPanel.add(new JLabel("Min random city link"));
        minRandomLinkField = new JTextField("0");
        fieldPanel.add(minRandomLinkField);

        labelPanel.add(new JLabel("Max random city link"));
        maxRandomLinkField = new JTextField("0");
        fieldPanel.add(maxRandomLinkField);

        labelPanel.add(new JLabel("Min random text paragraph"));
        minRandomTextField = new JTextField("0");
        fieldPanel.add(minRandomTextField);

        labelPanel.add(new JLabel("Max random text paragraph"));
        maxRandomTextField = new JTextField("0");
        fieldPanel.add(maxRandomTextField);

        panel.add(labelPanel, BorderLayout.WEST);
        panel.add(fieldPanel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.EAST);
        add(panel, BorderLayout.CENTER);


        generateButton = new JButton("Generate");
        generateButton.addActionListener(this);
        add(generateButton, BorderLayout.SOUTH);

        progressBar = new JProgressBar();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        final Object source = e.getSource();
        if (sourceDirButton == source) {
            JFileChooser jFileChooser = new JFileChooser(sourceDirField.getText());
            jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                sourceDirField.setText(jFileChooser.getSelectedFile().getAbsolutePath());
            }
        } else if (targetDirButton == source) {
            JFileChooser jFileChooser = new JFileChooser(targetDirField.getText());
            jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                targetDirField.setText(jFileChooser.getSelectedFile().getAbsolutePath());
            }
        } else if (generateButton == source) {
            remove(generateButton);
            add(progressBar, BorderLayout.SOUTH);
            getRootPane().revalidate();

            String sourceDir = sourceDirField.getText();
            boolean includeTest = includeTestCheckBox.isSelected();
            boolean includeWordData = includeWordDataCheckBox.isSelected();
            boolean includeUsaData = includeUsaDataCheckBox.isSelected();
            boolean includeMoneyGramData = includeMoneyGramDataCheckBox.isSelected();
            String targetDir = targetDirField.getText();
            int indexPageSize = Integer.parseInt(indexPageSizeField.getText());
            int countryPageSize = Integer.parseInt(countryPageSizeField.getText());
            int cityPageSize = Integer.parseInt(cityPageSizeField.getText());
            int minRandomLink = Integer.parseInt(minRandomLinkField.getText());
            int maxRandomLink = Integer.parseInt(maxRandomLinkField.getText());
            int minRandomText = Integer.parseInt(minRandomTextField.getText());
            int maxRandomText = Integer.parseInt(maxRandomTextField.getText());
            SiteGen siteGen = new SiteGen(sourceDir, includeTest, includeWordData, includeUsaData, includeMoneyGramData, targetDir, indexPageSize, countryPageSize, cityPageSize, minRandomLink, maxRandomLink, minRandomText, maxRandomText);
            progressBar.setModel(siteGen);
            siteGen.addChangeListener(this);
            new Thread(siteGen).start();
        }
    }

    public void stateChanged(ChangeEvent e) {
        Object source = e.getSource();
        if (source instanceof SiteGen) {
            if (((SiteGen) source).getValue() == ((SiteGen) source).getMaximum()) {
                remove(progressBar);
                add(generateButton, BorderLayout.SOUTH);
                getRootPane().revalidate();
                repaint();
                File file = new File(targetDirField.getText(), "index.html");
                if (file.exists() && Desktop.isDesktopSupported()) {
                    Desktop desktop = Desktop.getDesktop();
                    if (desktop.isSupported(Desktop.Action.OPEN)) {
                        try {
                            desktop.open(file);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public static void main(String args[]) {
		new SiteGenFrame();
    }
}
